package com.infomanage.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.infomanage.bean.SearchBean;
import com.infomanage.exception.DaoException;
import com.infomanage.po.HibernateSessionFactory;
import com.infomanage.po.User;

public class UserDAO extends BaseDAO<User>{
	
	public User findUserByName(String username) throws DaoException{
		Session session = HibernateSessionFactory.getSession();
		List<User> list = null;
		try {
			Query query =  session.createQuery("from User where userName = ?");
			query.setString(0, username);
			list = query.list();
		} catch (Exception e) {
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
		return (list == null || list.size()==0) ? null : list.get(0);
	}
	
	public int count(SearchBean searchBean) throws DaoException{
		return super.count(User.class, searchBean);
	}
	
	public List<User> findByFields(SearchBean searchBean) throws DaoException {
		return super.findByFields(User.class, searchBean);
	}
	
	public boolean isExistUsername(String username) throws DaoException{
		if(findUserByName(username) != null)
			return true;
		return false;
	}
}
